package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.yx.cloud.R;
import com.yx.cloud.fragment.MusicListFragment;
import com.yx.cloud.product.sdk.tmall.MusicInfo;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.view.ProgressBarView;
import com.iot.common.utils.LogUtils;;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MusicListAdapter extends RecyclerView.Adapter implements View.OnClickListener{
    private static final String TAG = "MusicListAdapter";
    private Context context;
    private List<MusicInfo> mMusicInfoList;
    private InnerItemOnclickListener listener;
    private int currentPos = -1;
    private int musicPlaying = -1;
    private boolean changePlayBtnShow = false;
    private Set<String> iconUriSet;
    private RequestOptions options;
    private long cateId;

    public MusicListAdapter(Context context, List<MusicInfo> musicInfoList,long cateId) {
        this.context = context;
        this.mMusicInfoList = musicInfoList;
        this.iconUriSet = new HashSet<>();
        this.cateId = cateId;
        this.options = new RequestOptions()
                .placeholder(R.mipmap.music_cover_default)
                .bitmapTransform(new RoundedCorners(20));
    }

    public void  updateData(List<MusicInfo> musicInfoList){
        this.mMusicInfoList = musicInfoList;
        notifyDataSetChanged();
    }

    public int getCurrentPos() {
        return currentPos;
    }

    public void setCurrentPos(int currentPos) {
        this.currentPos = currentPos;
    }

    public void  selectItem(int position){
        currentPos = position;
        notifyDataSetChanged();
    }

    public int getSelectedItem(){
        return currentPos;
    }

    @Override
    public int getItemCount() {
        return mMusicInfoList.size();
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        changePlayBtnShow = true;
        currentPos = (int)v.getTag();
        listener.itemClick(v,(int)v.getTag());
    }

    public MusicInfo getItem(int position){
        return mMusicInfoList.get(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.music_playing_list_item,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MusicInfo musicInfo = mMusicInfoList.get(position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.rlMusicItem.setOnClickListener(this);
        myViewHolder.rlMusicItem.setTag(position);

        if(currentPos == position){
            if(changePlayBtnShow){
                changePlayBtnShow(myViewHolder,musicInfo);
                changePlayBtnShow = false;
            }else {
                refreshPlayBtnShow(myViewHolder,musicInfo);
            }
        }else {
            myViewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
            myViewHolder.progressBarView.setProgress(0);
            showPlayingAnimation(myViewHolder,false);
        }

        String icon = musicInfo.getCoverUrl();
        if(icon == null || icon.equals("")){
            myViewHolder.ivMusicIcon.setImageResource(R.mipmap.music_cover_default);
        }else {
            Glide.with(context).load(icon).apply(options).into(myViewHolder.ivMusicIcon);
        }
        myViewHolder.tvMusicName.setText(musicInfo.getTitle());
        myViewHolder.tvSinger.setText(musicInfo.getSingers());

        myViewHolder.tvMusicName.setText(musicInfo.getTitle());
        myViewHolder.tvSinger.setText(musicInfo.getSingers());

        if(cateId == MusicListFragment.CATE_TYPE_SEARCH_ALBUM){
            myViewHolder.ivAlbumBg.setVisibility(View.VISIBLE);
        }

        Log.d("music","play position + " + position);
    }

    private void showPlayingAnimation(MusicListAdapter.MyViewHolder viewHolder,boolean show){
        if(show){
            viewHolder.ivMusicPlay.setVisibility(View.VISIBLE);
            viewHolder.progressBarView.setVisibility(View.INVISIBLE);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing_animation);
        }else {
            viewHolder.ivMusicPlay.setVisibility(View.INVISIBLE);
            viewHolder.progressBarView.setVisibility(View.INVISIBLE);
        }
    }

    private void changePlayBtnShow(MusicListAdapter.MyViewHolder viewHolder,MusicInfo musicInfo){
        if(musicInfo.getMusicPlayStatus() == MusicInfo.STATE_PAUSE){
            Log.d("music","set music playing");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PLAY);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing_animation);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            musicPlaying = 1;
            //播放完毕
            if(musicInfo.getProgress() == 100){
                viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
                musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
                viewHolder.progressBarView.setProgress(0);
                musicPlaying = -1;
                showPlayingAnimation(viewHolder,false);
            }
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PLAY)){
            Log.d("music","set music stop");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PAUSE);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = 0;
            showPlayingAnimation(viewHolder,false);
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_IDLE)){
            Log.d("music","set music playing");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PLAY);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing_animation);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            musicPlaying = 1;
        }
    }

    private void refreshPlayBtnShow(MusicListAdapter.MyViewHolder viewHolder,MusicInfo musicInfo){
        if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PAUSE)){
            Log.d("music","set music stop");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = 0;
            showPlayingAnimation(viewHolder,false);
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PLAY)){
            Log.d("music","set music playing");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing_animation);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            showPlayingAnimation(viewHolder,true);
            musicPlaying = 1;
            //播放完毕
            if(musicInfo.getProgress() == 100){
                viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
                musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
                viewHolder.progressBarView.setProgress(0);
                musicPlaying = -1;
                showPlayingAnimation(viewHolder,false);
            }
        }else {
            Log.d("music","reset music");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = -1;
            showPlayingAnimation(viewHolder,false);
        }
    }


    public void updateMusic(List<MusicInfo> musicInfoList) {
        mMusicInfoList = musicInfoList;
        notifyDataSetChanged();
        loadCover(musicInfoList);
    }

    public void addMusic(List<MusicInfo> musicInfoList) {
        if(musicInfoList == null){
            return;
        }
        mMusicInfoList.addAll(musicInfoList);
        notifyDataSetChanged();
        loadCover(musicInfoList);
    }

    private void loadCover(List<MusicInfo> musicInfoList){
        iconUriSet.clear();
        LogUtils.i("load image uri size : " + iconUriSet.size());
        for (int i = 0; i < musicInfoList.size(); i++) {
            if(musicInfoList.get(i).getCoverUrl() != null){
                iconUriSet.add(musicInfoList.get(i).getCoverUrl());
            }
        }
        LogUtils.i("load image uri size1 : " + iconUriSet.size());
    }

    public List<MusicInfo> getmMusicInfoList() {
        return mMusicInfoList;
    }

    public void setmMusicInfoList(List<MusicInfo> mMusicInfoList) {
        this.mMusicInfoList = mMusicInfoList;
    }


    public int getMusicPlaying() {
        return musicPlaying;
    }

    public void setMusicPlaying(int musicPlaying) {
        this.musicPlaying = musicPlaying;
    }

    public boolean isChangePlayBtnShow() {
        return changePlayBtnShow;
    }

    public void setChangePlayBtnShow(boolean changePlayBtnShow) {
        this.changePlayBtnShow = changePlayBtnShow;
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v,int position);
    }

    public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    public void clearData(){
        if(mMusicInfoList != null && mMusicInfoList.size() > 0){
            mMusicInfoList.clear();
        }
    }

    class MyViewHolder extends  RecyclerView.ViewHolder{
        RelativeLayout rlMusicItem;
        FrameLayout flMusicPlay;
        ImageView ivMusicIcon;
        TextView tvMusicName;
        TextView tvSinger;
        ProgressBarView progressBarView;
        ImageView ivMusicPlay;
        ImageView ivAlbumBg;
        public MyViewHolder(View itemView) {
            super(itemView);
            rlMusicItem = itemView.findViewById(R.id.rl_music_item);
            flMusicPlay = itemView.findViewById(R.id.fl_play);
            progressBarView = itemView.findViewById(R.id.progress_view);
            ivMusicIcon = itemView.findViewById(R.id.iv_music_item_icon);
            tvMusicName = itemView.findViewById(R.id.tv_music_name);
            tvSinger = itemView.findViewById(R.id.tv_singer);
            ivMusicPlay = itemView.findViewById(R.id.iv_play);
            ivAlbumBg = itemView.findViewById(R.id.iv_album_bg);
        }
    }

}
